/**
 * 获取路由
 * @param path
 * @param pages
 * @returns {undefined|*}
 */
function getPageRouteByPath (path, pages) {
  const paths = path.split('-')
  const pathName = paths.shift()
  const page = pages.find(
    item => item.name === pathName || (item.meta && item.meta.title === pathName));
  if (paths.length) {
    const name = getPageRouteByPath(paths.join('-'), page.children)
    if (name) {
      return name
    }
  } else {
    return page.name
  }
}

/**
 * 获取模块名称
 * @param modelId
 * @param pages
 * @returns {undefined|*}
 */
function getPageRouteByName (modelId, pages) {
  for(const page of pages) {
    if (page.meta && page.meta.id === modelId) {
      return page.meta.title;
    }
    if(page.children) {
      const title = getPageRouteByName(modelId, page.children)
      if (title) {
        return title;
      }
    }
  }
}

function judgeFieldCorrection (model, updatePackage) {
  if (model && model.includes('.')) {
    const tableAndField = model.split('.')
    const fields = updatePackage[tableAndField[0]]
    if (fields) {
      return fields.includes(tableAndField[1])
    }
    return false
  }
  return false
}

const getters = {
  getSidebar: state => state.nav.sidebar,
  multiTab: state => {},
  getHeaderTab: (state) => (group) => state.nav.headerTab ? state.nav.headerTab[group] || [] : [],
  getNavList: state => state.permission.navList,
  addRouters: state => state.permission.addRouters,
  getUserInfo: state => state.user.userInfo,
  getBtnAuth: state => authGroupName => state.permission.authList.filter(item => item.name === authGroupName),
  getUserUnit: state => state.user.userUnit,
  getUserOrg: state => state.user.userOrg,
  getMenuList: state => state.permission.menuList,
  getPageRouteByPath: state => (path) => getPageRouteByPath(path, state.permission.addRouters),
  getPageRouteByName: state => (modelId) => getPageRouteByName(modelId, state.permission.addRouters),
  getReportLogConfig: state => state.user.reportLogConfig,
  getDataMode: state=> state.dataMode
  // judgeFieldCorrection: state => (model) => judgeFieldCorrection(model, state.permission.updatePackage)
}

export default getters
